package ucap.com.lesanapp.utils;

import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import java.util.ArrayList;

import ucap.com.lesanapp.bean.DateBean;
import ucap.com.lesanapp.global.Constants;
import ucap.com.lesanapp.view.UpdataNewLoadingDialog;

/**
 * Created by wekingwang on 16/7/29.
 * 获取服务器数据
 */
public class GetDateForServiceUtils {
    Activity mActivity;
    BaseAdapter mAdapter;
    ArrayList<DateBean.RowsBean> mListFocusNews;
    ArrayList<DateBean.RowsBean> mListVideo;
    ArrayList<DateBean.RowsBean> mListPolicy;
    SwipeRefreshLayout srRefresh;
    ListView lvTopNewsContent;
    TextView tvClickLoading;
    public final UpdataNewLoadingDialog loadingDialog;

    public GetDateForServiceUtils(Activity mActivity,
                                  BaseAdapter mAdapter,
                                  ArrayList<DateBean.RowsBean> mListFocusNews,
                                  ArrayList<DateBean.RowsBean> mListVideo,
                                  ArrayList<DateBean.RowsBean> mListPolicy,
                                  SwipeRefreshLayout srRefresh, ListView lvTopNewsContent, TextView tvClickLoading) {
        this.mActivity = mActivity;
        this.mAdapter = mAdapter;
        this.mListFocusNews = mListFocusNews;
        this.mListVideo = mListVideo;
        this.mListPolicy = mListPolicy;
        this.srRefresh = srRefresh;
        this.lvTopNewsContent = lvTopNewsContent;
        this.tvClickLoading = tvClickLoading;
        loadingDialog = new UpdataNewLoadingDialog(mActivity);
        loadingDialog.setCancelable(false);

    }

    /**
     * 解析数据
     *
     * @param date
     * @param list
     */
    private void fromjson(String date, ArrayList<DateBean.RowsBean> list) {
        Gson gson = new Gson();
        DateBean dateBean = gson.fromJson(date, DateBean.class);
        list.addAll(dateBean.getRows());
    }

    /**
     * 获取要闻的数据
     *
     * @param http
     */
    public void getDateFocusNews(final HttpUtils http) {
        http.send(HttpMethod.GET, Constants.URL_FOCUS_NEWS, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.show();
                mListFocusNews.clear();//加载数据之前,先清空集合里的数据,避免出现重复数据
                mListVideo.clear();//加载数据之前,先清空集合里的数据,避免出现重复数据
                mListPolicy.clear();//加载数据之前,先清空集合里的数据,避免出现重复数据
                fromjson(responseInfo.result, mListFocusNews);
                Log.i("jsonFocusNew", responseInfo.result);
                getDateVideo(http);
                srRefresh.setRefreshing(false);
                tvClickLoading.setVisibility(View.GONE);

            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(mActivity, "网络异常!", Toast.LENGTH_SHORT).show();
                srRefresh.setRefreshing(false);
                tvClickLoading.setVisibility(View.VISIBLE);
                loadingDialog.dismiss();
            }
        });
    }

    /**
     * 视频
     *
     * @param http
     */
    public void getDateVideo(final HttpUtils http) {
        http.send(HttpMethod.GET, Constants.URL_VIDEO, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                fromjson(responseInfo.result, mListVideo);
                Log.i("jsonBranch", responseInfo.result);
                getDatePolicy(http);
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }


    /**
     * 政策解读
     *
     * @param http
     */

    public void getDatePolicy(HttpUtils http) {
        http.send(HttpMethod.GET, Constants.URL_POLICY, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                fromjson(responseInfo.result, mListPolicy);
                Log.i("jsonTour", responseInfo.result);
                mAdapter.notifyDataSetChanged();
                lvTopNewsContent.setAdapter(mAdapter);
                srRefresh.setVisibility(View.VISIBLE);
                loadingDialog.dismiss();
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }


}
